# -*- coding: UTF8 -*-
import os,time,datetime
import multiprocessing
from ftplib import FTP
monthes = ['201706']#the data of which month you want download
savepath='G:/Himawari_L1_grid_data/201706/'
def SAT_data_download():
    ftp = FTP()
    ftp.set_debuglevel(2)
    ftp.connect('ftp.ptree.jaxa.jp')
    ftp.login('562929453_qq.com', 'SP+wari8')#username and password
    for month in monthes:
        ftp.cwd('/jma/netcdf/'+month)
        folders=ftp.nlst()#day
        for folder in folders:#days
            remotepath = '/jma/netcdf/' + month+'/'+folder
            ftp.cwd(remotepath)
            filenames = ftp.nlst()
            for filename in filenames:#NC_H08_20170602_0150_R21_FLDK.02401_02401.nc
                hour_min=filename.split('_')[3]
                hour_num=int(hour_min[:2])
                if '02401_02401.nc' in filename:#we download the date with resolution of 0.05 degree
                    if 1<=hour_num<=10:
                        localpath = 'G:/Himawari_L1_grid_data/'+ month + '/' + filename#the local save position
                        if not os.path.exists(localpath):
                            file_handle = open(localpath, "wb").write  # 以写模式在本地打开文件
                            ftp.retrbinary('RETR ' + remotepath + '/' + filename, file_handle)
    ftp.quit()

def get_latest_file_time():
    files=os.listdir(savepath)
    latest_time=datetime.datetime(2000,1,1,1,1)
    latest_filename=''
    for file in files:
        path = os.path.join(savepath, file)
        timestamp = os.path.getmtime(path)
        date = datetime.datetime.fromtimestamp(timestamp)
        if date>latest_time:
            latest_time=date
            latest_filename=file
    return latest_time,latest_filename

def time_judge():
    while 1:
        latest_time,latest_filename=get_latest_file_time()
        nowtime=datetime.datetime.now()
        deltaT=nowtime-latest_time
        print('上次更新距离现在时间：',deltaT,latest_filename)
        if deltaT.total_seconds()>40*60:#如果超过n分钟无更新
            break
        else:
            pass
        time.sleep(30)


if __name__=='__main__':
    p_download = multiprocessing.Process(target=SAT_data_download)
    p_timecheck = multiprocessing.Process(target=time_judge)
    p_download.start()
    p_timecheck.start()
    while 1:
        if not p_timecheck.is_alive():#如果n分钟无更新
            p_download.terminate()
            # latest_time, latest_filename = get_latest_file_time()
            # os.remove(savepath+latest_filename)
            p_download = multiprocessing.Process(target=SAT_data_download)
            p_timecheck = multiprocessing.Process(target=time_judge)
            p_download.start()
            p_timecheck.start()
        time.sleep(30)



